//
//  HMOrderController.m
//  HMMarket
//
//  Created by 孙鹏飞 on 2017/5/9.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMOrderController.h"
#import "HMAllOrderController.h"
#import "HMForGoodController.h"
#import "HMPayOrderController.h"

@interface HMOrderController () <UIScrollViewDelegate>

/** 标签栏底部的红色指示器 */
@property (nonatomic, weak) UIView * indicatorView;
/** 当前选中的按钮 */
@property (nonatomic, weak) UIButton * selectedButton;
/** 顶部的所有标签 */
@property (nonatomic, weak) UIView * titlesView;
/** 底部的所有内容 */
@property (nonatomic, weak) UIScrollView *contentView;

@end

@implementation HMOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航栏
    [self setupNav];
    
    // 初始化子控制器
    [self setupChildVces];
    
    // 设置顶部的标签栏
    [self setupTitlesView];
    
    // 设置底部的scrollView
    [self setupContentView];
    
    // 第一次进来显示第几个控制器
    [self titleClick:self.titlesView.subviews[_pageIndex]];
}

/**
 *
 */
-(void)setupChildVces
{
    HMAllOrderController * all = [[HMAllOrderController alloc] init];
    all.NavigationBarHidden = NO;
    [self addChildViewController:all];
    
    HMForGoodController * forGood = [[HMForGoodController alloc] init];
    forGood.NavigationBarHidden = NO;
    [self addChildViewController:forGood];
    
    HMPayOrderController * pay = [[HMPayOrderController alloc] init];
    pay.NavigationBarHidden = NO;
    [self addChildViewController:pay];
}

/**
 *  设置顶部的标签栏
 */
-(void)setupTitlesView
{
    //标签栏整体
    UIView * titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [kBackGolor colorWithAlphaComponent:1];
    titlesView.width = self.view.width;
    titlesView.height = 40;
    titlesView.y = 64;
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    // 底部的红色指示器
    UIView * indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = kMainColor;
    indicatorView.height = 2;
    indicatorView.tag = -1;
//    indicatorView.y = titlesView.height - indicatorView.height;
    indicatorView.y = titlesView.height;
    self.indicatorView = indicatorView;
    
    // 内部的子标签
    NSArray * titles = @[@"全部",@"待收货",@"待付款"];
    CGFloat width = titlesView.width / titles.count;
    CGFloat height = titlesView.height - 5;
    for (NSInteger i = 0; i < titles.count; i++) {
        UIButton * button = [[UIButton alloc] init];
        button.tag = i;
        button.height = height;
        button.width = width;
        button.x = i * width;
        button.y = 5;
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button layoutIfNeeded]; //强制布局（强制更新子控件的frame）
        button.backgroundColor = [UIColor whiteColor];
        [button setTitleColor:kTextColor forState:UIControlStateNormal];
        [button setTitleColor:kMainColor forState:UIControlStateDisabled];
        button.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:button];
        
        // 默认点击了第一个按钮
        if (i == 0) {
            button.enabled = NO;
            self.selectedButton = button;
            
            // 让按钮内部的label根据文字内容来计算尺寸
            [button.titleLabel sizeToFit];
            self.indicatorView.width = button.titleLabel.width;
            self.indicatorView.centerX = button.centerX;
        }
    }
    [titlesView addSubview:indicatorView];
    
}

-(void)titleClick:(UIButton *)button
{
    // 修改按钮状态
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    self.selectedButton = button;
    
    // 动画
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = button.titleLabel.width;
        self.indicatorView.centerX = button.centerX;
    }];
    
    // 滚动
    CGPoint offset = self.contentView.contentOffset;
    offset.x = button.tag * self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES];
}

/**
 * 设置底部的scrollView
 */
-(void)setupContentView
{
    // 不要自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView * contentView = [[UIScrollView alloc] init];
    contentView.frame = self.view.bounds;
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    [self.view insertSubview:contentView atIndex:0];
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
    self.contentView = contentView;
    
    // 添加第一个控制器的view
    [self scrollViewDidEndScrollingAnimation:contentView];
}

/**
 * 设置导航栏
 */
-(void)setupNav
{
    self.title = @"我的订单";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [self.RightBarItem setImage:[UIImage imageNamed:@"telephone_52"] forState:UIControlStateNormal];
    [self.RightBarItem addTarget:self action:@selector(telBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)telBtnClick
{
    MyLog(@"打电话");
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"4000999168"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}


#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 当前的索引
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    
    // 取出子控制器
    UITableViewController * vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = 0; // 设置控制器view的y值为0 （默认是20）
    vc.view.height = scrollView.height; // 设置控制器view的高度为整个屏幕的高度（默认是比屏幕高度少20）
    // 设置内边距
    CGFloat bottom = self.tabBarController.tabBar.height;
    CGFloat top = CGRectGetMaxY(self.titlesView.frame);
    vc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    // 设置滚动条的内边距
    vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
    [scrollView addSubview:vc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
    // 点击按钮
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    [self titleClick:self.titlesView.subviews[index]];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if ([self.viewIden isEqualToString:@"MGOrderViewController"]) {
        //        NSInteger count = self.navigationController.viewControllers;
//        UIViewController *vc = [self.navigationController.viewControllers firstObject];
        [self.navigationController popToRootViewControllerAnimated:NO];
    }
}

@end
